package cn.exrick.xboot.vip.controller;

import cn.exrick.xboot.core.common.utils.PageUtil;
import cn.exrick.xboot.core.common.utils.ResultUtil;
import cn.exrick.xboot.core.common.utils.SecurityUtil;
import cn.exrick.xboot.core.common.vo.PageVo;
import cn.exrick.xboot.core.common.vo.Result;
import cn.exrick.xboot.core.entity.Member;
import cn.exrick.xboot.core.service.MemberService;
import cn.exrick.xboot.vip.entity.VipCard;
import cn.exrick.xboot.vip.service.IVipCardService;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* <p>
    * 会员卡  前端控制器
    * </p>
*
* @author yuhaifei
* @since 2022-10-09
*/
@RestController
@RequestMapping("/xboot/vipcard")
public class VipCardController {

    @Autowired
    private IVipCardService vipCardService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private SecurityUtil securityUtil;

    /**
     * 获取会员卡列表
     * @param pageVo
     * @param vipCard
     * @return
     */
    @GetMapping("page")
    public Result<IPage<VipCard>> page(PageVo pageVo, VipCard vipCard){
        Page page = PageUtil.initMpPage(pageVo);
        QueryWrapper<VipCard> wrapper = new QueryWrapper<VipCard>();
        this.setWapper(vipCard, wrapper);
        IPage<VipCard> ipage = vipCardService.page(page, wrapper);
        return new ResultUtil<IPage<VipCard>>().setData(ipage);

    }

    public void setWapper(VipCard vipCard, QueryWrapper wrapper){
        if(StrUtil.isNotBlank(vipCard.getRealName())){
            wrapper.like("real_name", vipCard.getRealName());
        }
        if(StrUtil.isNotBlank(vipCard.getIdcard())){
            wrapper.like("id_card", vipCard.getIdcard());
        }
    }

    /**
     * 生成卡号
     * @return
     */
    @GetMapping("generateCardId")
    public Result generateCardId(){
        return ResultUtil.data(IdUtil.simpleUUID());
    }

    /**
     * 新增会员卡
     * @param vipCard
     * @return
     */
    @PostMapping("addVipCard")
    public Result addVipCard(VipCard vipCard){
        // 设置卡号
        if(StrUtil.isBlank(vipCard.getCardId())){
            vipCard.setCardId(IdUtil.simpleUUID());
        }
        // 设置openid
        if(StrUtil.isBlank(vipCard.getOpenId())){
            Member m = memberService.get(vipCard.getMemberId().toString());
            vipCard.setOpenId(m.getOpenId());
        }
        vipCardService.save(vipCard);
        return ResultUtil.data(vipCard.getRealName() + "添加成功");
    }

    /**
     * 编辑会员卡
     * @param vipCard
     * @return
     */
    @PostMapping("editVipCard")
    public Result editVipCard(VipCard vipCard){
        VipCard vcard = vipCardService.getById(vipCard.getId());
        BeanUtils.copyProperties(vipCard, vcard);
        vipCardService.updateById(vcard);
        return ResultUtil.data(vipCard.getRealName()+"编辑成功");
    }

    /**
     * 删除会员卡
     * @param id
     * @return
     */
    @PostMapping("deleteVipCard")
    public Result deleteVipCard(String id){
        vipCardService.removeById(id);
        return ResultUtil.data("删除成功");
    }

    /**
     * 获取会员卡信息
     * @return
     */
    @GetMapping("getVipCardInfo")
    public Result getVipCardInfo(){
        VipCard card = vipCardService.getOne(new QueryWrapper<VipCard>()
                .eq("member_id", securityUtil.getCurrMember().getId()));
        return ResultUtil.data(card);
    }

}
